package com.maori.languageapp.consts;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CaptureFilterAdapter {

	/*
	 * _id;LID;NID,IdiomID ;Idiom ;PhraseID ;Phrase ;SongID ;song_title
	 * ;song_content ;StoryID ; story_title ;story_content ;WordID ;word
	 */

	public static final String KEY_ID = "_id";
	public static final String KEY_LID = "LID";
	public static final String KEY_NID = "NID";
	public static final String KEY_IdiomID = "IdiomID";
	public static final String KEY_Idiom = "Idiom";
	public static final String KEY_PhraseID = "PhraseID";
	public static final String KEY_Phrase = "Phrase";

	public static final String KEY_SongIDe = "SongID";
	public static final String KEY_song_title = "song_title";
	public static final String KEY_song_content = "song_content";
	public static final String KEY_StoryID = "StoryID";
	public static final String KEY_story_title = "story_title";
	public static final String KEY_story_content = "story_content";
	public static final String KEY_WordID = "WordID";
	public static final String KEY_word = "word";

	private static final String TAG = "CaptureFilterAdapter";
	private DatabaseHelper mDbHelperItems;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "maorilanguageapp.db";
	private String PHRASE_TABLE = "Phrase";
	private String SONG_TABLE = "Song";
	private String STORY_TABLE = "Story";
	private String WORD_TABLE = "Word";
	private String IDIOM_TABLE = "Idiom";
	private static final int DATABASE_VERSION = 1;

	private final Context mCtx;
	int c;
	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// Log.w(TAG, DATABASE_CREATE);
			// db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		}
	}

	public CaptureFilterAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public CaptureFilterAdapter open() throws SQLException {
		mDbHelperItems = new DatabaseHelper(mCtx);
		mDb = mDbHelperItems.getWritableDatabase();
		return this;
	}

	public void close() {
		if (mDbHelperItems != null) {
			mDbHelperItems.close();
		}
	}

	public Cursor fetchItemsByName(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(WORD_TABLE, new String[] { KEY_ID, KEY_LID,
					KEY_NID, KEY_WordID, KEY_word }, null, null, null, null,
					null);

		} else {
			mCursor = mDb.query(true, WORD_TABLE, new String[] { KEY_ID,
					KEY_LID, KEY_NID, KEY_WordID, KEY_word }, KEY_word
					+ " like '%" + inputText + "%'", null, null, null, null,
					null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// _id ,WordID ,LID ,NID ,word
	public Cursor fetchAllItems() {

		/*String query = "SELECT WordID,LID,NID,word FROM Word";
		// Cursor mCursor = mDb.query();
		Cursor mCursor = mDb.rawQuery(query, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;*/
		
		c = c+1;
		Log.v(TAG, "DJ-Filter Words :"+c);
		Cursor mCursor = mDb.query(WORD_TABLE, new String[] { KEY_ID,
				KEY_LID, KEY_NID, KEY_WordID, KEY_word }, null, null,
				null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	public Cursor fetchItemsByNameSparm(String inputText, String userLocCode)
			throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			/*
			 * mCursor = mDb.query(SQLITE_TABLE_ITEMS, new String[] {
			 * KEY_ITEMROWID, KEY_ITEMCODE, KEY_ITEMNAME, KEY_ITEMAVGPRICE,
			 * KEY_ITEMBRAND }, null, null, null, null, null);
			 */

			String query = "SELECT item._id,item.ItemCode,item.ItemName,item.AvgPrice,item.SBrandCode,floc.LocCode,floc.ItemCode,floc.QOH FROM fItems item,fItemLoc floc WHERE item.ItemCode = floc.ItemCode AND floc.LocCode = '"
					+ userLocCode + "' ORDER BY OrdSeq ASC";
			// Cursor mCursor = mDb.query();
			mCursor = mDb.rawQuery(query, null);

		} else {
			/*
			 * mCursor = mDb.query(true, SQLITE_TABLE_ITEMS, new String[] {
			 * KEY_ITEMROWID, KEY_ITEMCODE, KEY_ITEMNAME, KEY_ITEMAVGPRICE,
			 * KEY_ITEMBRAND }, KEY_ITEMNAME + " like '%" + inputText + "%'",
			 * null, null, null, null, null);
			 */
			String query = "SELECT item._id,item.ItemCode,item.ItemName,item.AvgPrice,item.SBrandCode,floc.LocCode,floc.ItemCode,floc.QOH FROM fItems item,fItemLoc floc WHERE item.ItemCode = floc.ItemCode AND floc.LocCode = '"
					+ userLocCode
					+ "' AND item.ItemName LIKE '%"
					+ inputText
					+ "%' ORDER BY OrdSeq ASC";
			// Cursor mCursor = mDb.query();
			mCursor = mDb.rawQuery(query, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// Squry execute for Secondry Invoice
	public Cursor fetchAllItemsparm(String userLocCode) {

		String query = "SELECT item._id,item.ItemCode,item.ItemName,item.AvgPrice,item.SBrandCode,floc.LocCode,floc.ItemCode,floc.QOH FROM fItems item,fItemLoc floc WHERE item.ItemCode = floc.ItemCode AND floc.LocCode = '"
				+ userLocCode + "' ORDER BY OrdSeq ASC";
		// Cursor mCursor = mDb.query();
		Cursor mCursor = mDb.rawQuery(query, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

}
